using Catalog.Application.CatalogItems.Interfaces;
using Catalog.Application.Interfaces;
using Catalog.Domain.Entities;
using MediatR;

namespace Catalog.Application.CatalogItems.Commands;

public class CreateCatalogItemCommandHandler : IRequestHandler<CreateCatalogItemCommand, Guid>
{
    private readonly ICatalogItemRepository _repository;
    private readonly IUnitOfWork _unitOfWork;

    public CreateCatalogItemCommandHandler(ICatalogItemRepository repository, IUnitOfWork unitOfWork)
    {
        _repository = repository;
        _unitOfWork = unitOfWork;
    }

    public async Task<Guid> Handle(CreateCatalogItemCommand request, CancellationToken cancellationToken)
    {
        var item = CatalogItem.Create(
            request.Name,
            request.Description,
            request.Price,
            request.AvailableStock,
            request.CategoryId,
            request.BrandId
        );
         _repository.Add(item);
        await _unitOfWork.SaveChangesAsync(cancellationToken);
        return item.Id;
    }
}